package com.flute.haflute.jobbox.common;

import java.rmi.RemoteException;
import java.util.List;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.flute.haflute.jobbox.base.CloudResourceBase;

/**
 * 系统实现的默认Mem数据资源管理器原型
 */
public class DefaultMemInputResource extends CloudResourceBase {
	private static Logger logger = LoggerFactory
			.getLogger(DefaultMemInputResource.class);
	private static final long serialVersionUID = -835563141249746822L;

	/** 数据 */
	Vector<String> resource = new Vector<String>();

	/** 数据分割粒度 */
	int nSplits = 2;

	// 本资源的ID
	String sId = "id";

	public DefaultMemInputResource() throws RemoteException {
		super();
		for (int i = 0; i < 20; i++)
			resource.add("www.sina.com");
	}

	@SuppressWarnings("unchecked")
	@Override
	public void addData(Object content) throws RemoteException {
		if (content instanceof List) {
			resource.addAll((List<String>) content);
			logger.debug("addContent size=" + resource.size());
			logger.debug("this ==" + this.toString());
		} else
			resource.add(content.toString());

	}

	@Override
	public synchronized Object getData(String taskNo) throws RemoteException {
		if (resource.size() == 0)
			return "no Data now, you may put some in me";
		return resource.remove(0);
	}

	// 设置数据分割粒度
	public void setTotalSplitNo(int splits) throws RemoteException {
		this.nSplits = splits;
	}

	public void setId(String sId) throws RemoteException {
		this.sId = sId;
	}

	@Override
	public String getId() throws RemoteException {
		return String.valueOf(System.currentTimeMillis());
	}

}
